package com.sfh.base.ui.dialog;

import android.app.Activity;
import android.widget.Toast;

import com.sfh.base.mvp.IView;


/**
 * 功能描述:
 *
 * @author SunFeihu 孙飞虎
 * @company 中储南京智慧物流科技有限公司
 * @copyright （版权）中储南京智慧物流科技有限公司所有
 * @date 2016/11/15
 */

public class AppStyleView implements IView {

    private Activity activity;

    private AppDialog.ToastDialog alertDialog;

    private  AppDialog.ProgressDialog  progressDialog;

    public AppStyleView(Activity activity) {

        this.activity = activity;
    }

    @Override
    public void showLoading(String message, boolean cancelAble, boolean... isBgColor) {

        this.hideLoading ();
        //创建加载进度对话框
        if (this.progressDialog == null) {
            this.progressDialog = new AppDialog.ProgressDialog (activity);
        }

        boolean color = (isBgColor != null && isBgColor.length > 0) ? isBgColor[0]: true;
        this.progressDialog.setCancelable(cancelAble);
        this.progressDialog.setMessage (message);
        this.progressDialog.setBackgroundColor(color);
        this.progressDialog.show ();
    }

    @Override
    public void hideLoading() {

        if (null != progressDialog && progressDialog.isShowing ()) {
            progressDialog.dismiss ();
        }
    }

    @Override
    public void showDialog(IAlertTemple alert, boolean cancelAble) {

        this.hideDialog ();
        this.alertDialog  = new AppDialog.ToastDialog (activity);
        this.alertDialog.setTemple(alert);
        this.alertDialog.setCancelable(cancelAble);
        this.alertDialog.setGravity (alert.gravity ());
        this.alertDialog.show ();
    }

    @Override
    public void hideDialog() {

        if (null != this.alertDialog && this.alertDialog.isShowing ()) {
            this.alertDialog.dismiss ();
            this.alertDialog = null;
        }
    }

    Toast toast;

    @Override
    public void showToast(String message, int ms, int... gravity) {

        ms = ms == Toast.LENGTH_SHORT ? Toast.LENGTH_SHORT : Toast.LENGTH_LONG;
        if (this.toast == null) {
            this.toast = Toast.makeText (activity, message, ms);
        } else {
            this.toast.setText (message);
            this.toast.setDuration (ms);
        }
        if (gravity.length > 0) {
            this.toast.setGravity (gravity[0], 0, 10);
        }
        this.toast.show ();
    }


}
